/*Authors: Laura (everything except geolocation), 
 *         Odd-Arild (geolocation)
 * 
 */
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see a blank space instead of the map, this
// is probably because you have denied permission for location sharing.

    var map;

    function initialize() {

        var mapOptions = {zoom: 10};
        map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);

        // Try HTML5 geolocation
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var pos = new google.maps.LatLng(position.coords.latitude,
                        position.coords.longitude);

                // Infowindow showing user's location
                var infowindow = new google.maps.InfoWindow({
                    map: map,
                    position: pos,
                    content: 'Your location'
                });

                // Make "your location" marker disappear soon after hovering over map
                google.maps.event.addListener(map, 'mouseover', function() {
                    window.setTimeout(function() {
                        infowindow.close();
                    }, 1500);
                });

                // Query
                var request = {
                    location: pos,
                    radius: 80467, // 80467 meters = 50 miles
                    query: "hospital"  
                    //See: https://developers.google.com/appengine/docs/python/search/query_strings
                    // for more on query strings
                };
                var service = new google.maps.places.PlacesService(map);
                service.textSearch(request, callback);
                //service.nearbySearch( request, callback);

                map.setCenter(pos);
            }, function() {
                handleNoGeolocation(true);
            });
        } else {
            // Browser doesn't support Geolocation
            handleNoGeolocation(false);
        }
    }

    // Get all query results
    function callback(results, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            for (var i = 0; i < results.length; i++) {
                createMarker(results[i]);
            }
        }
    }

    // Place markers for all results
    function createMarker(place) {
        var infowindow;
        var marker = new google.maps.Marker({
            map: map,
            position: place.geometry.location
        });

        // Add infowindow with name or address for each marker on hover
        google.maps.event.addListener(marker, 'mouseover', function() {
            infowindow = new google.maps.InfoWindow({
                map: map,
                position: place.geometry.location,
                //content: place.formatted_address  
                content: place.name
            });
        });

        // Make infowindow disappear when not hovered over
        google.maps.event.addListener(marker, 'mouseout', function() {
            window.setTimeout(function() {
                infowindow.close();
            }, 1000);
        });
    }

    // Geolocation failure
    function handleNoGeolocation(errorFlag) {
        if (errorFlag) {
            var content = 'Error: The Geolocation service failed.';
        } else {
            var content = 'Error: Your browser doesn\'t support geolocation.';
        }

        var options = {
            map: map,
            position: new google.maps.LatLng(42.69, -73.75), // Albany, NY
            content: content
        };

        //var infowindow = new google.maps.InfoWindow(options);
        map.setCenter(options.position);
    }

    //Load the map on click of doctorButton. Lay map on top of slideshow.
    function load(){
        var canvas = document.getElementById( "map-canvas");
        document.getElementById("slideshow").style.visibility='hidden';
        canvas.style.border= "solid black 1px";
        initialize();
    }
